﻿using System;
using System.Windows.Forms;

namespace CodePlex.DnD4eCampaignManager.CampaignManagerControlLibrary
{
    public partial class ModalStringPicker : Form
    {
        private static ModalStringPicker _picker;

        public ModalStringPicker()
        {
            InitializeComponent();
        }

        private void ModalNumberPicker_Load(object sender, EventArgs e)
        {
            StringToPickTextbox.Select();
            StringToPickTextbox.Select(0, StringToPickTextbox.Text.Length);
        }

        private void SetButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void StringToPickTextbox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ( e.KeyChar == '\r' )
            {
                SetButton_Click(sender, e);
            }
        }

        public static string GetString(string title, string message, string suggestedValue, int width)
        {
            SetupPicker(title, message, suggestedValue, width);
            return GetUserResponse();
        }

        public static string GetString(string title, string message, string suggestedValue, int width, char passwordChar)
        {
            SetupPicker(title, message, suggestedValue, width);
            _picker.StringToPickTextbox.PasswordChar = passwordChar;
            return GetUserResponse();
        }

        private static string GetUserResponse()
        {
            _picker.ShowDialog();
            return _picker.StringToPickTextbox.Text;
        }

        private static void SetupPicker(string title, string message, string suggestedValue, int width)
        {
            _picker = new ModalStringPicker();
            _picker.Text = title;
            _picker.Width = width;
            _picker.StringToPickName.Text = message;
            _picker.StringToPickTextbox.Text = suggestedValue;
        }
    }
}